package com.gemme.gemme.view;

import android.content.Context;
import android.graphics.Paint;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.R;
import com.gemme.gemme.adapter.DataBean;
import com.gemme.gemme.adapter.Image2Adapter;
import com.gemme.gemme.adapter.ImageAdapter;
import com.gemme.gemme.home.ShopProductInfoBean;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.DpUtil;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HomeHeadView extends RelativeLayout {
    private Context context;
    public HomeHeadView(Context context) {
        this(context,null);
    }

    public HomeHeadView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public HomeHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    HomeHorSrollView limit_view;
    NewProductView new_view;
    HomeHorSrollView mai_view;
    Banner home_banner;
    View ll_limit_view;
    View ll_new_view;
    View ll_mai_view;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.define_head_view,this);

        home_banner = findViewById(R.id.home_banner);

        limit_view = findViewById(R.id.limit_view);

        new_view = findViewById(R.id.new_view);

        mai_view = findViewById(R.id.mai_view);


        ll_limit_view = findViewById(R.id.ll_limit_view);
        ll_new_view = findViewById(R.id.ll_new_view);
        ll_mai_view = findViewById(R.id.ll_mai_view);


        TextView tv_more_limit = findViewById(R.id.tv_more_limit);
        TextView tv_more_new = findViewById(R.id.tv_more_new);
        TextView tv_more_buy = findViewById(R.id.tv_more_buy);

    /*    tvTest.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvTest.getPaint().setAntiAlias(true);//抗锯齿*/

        tv_more_limit.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //下划线
        tv_more_new.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //下划线
        tv_more_buy.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //下划线
        getData();
    }


    List<String> picList = new ArrayList<>();//轮播图数据

/*
    private void initBanner() {
        if(picList == null)
            return;
        ArrayList<DataBean> dataBeans = new ArrayList<>();

//        CoordinatorLayout

        for (int i = 0; i < picList.size(); i++) {
            DataBean dataBean = new DataBean();
            dataBean.setImageRes(picList.get(i));
            dataBeans.add(dataBean);
        }
        home_banner
//                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setAdapter(new Image2Adapter(dataBeans))
                .setIndicator(new CircleIndicator(context))
//                .setIndicator(new RectangleIndicator(this))
                .isAutoLoop(false)
//                .setIndicatorMargins(new IndicatorConfig.Margins(0,0,0, DpUtil.dip2px(context,50)))
                .start();
    }
*/

    private void initBanner() {
        try {
            if(homeBannerBean == null){
                return;
            }
            ArrayList<DataBean> dataBeans = new ArrayList<>();
            List<HomeBannerBean.DataBean.RecordsBean> picList = homeBannerBean.getData().getRecords();
            for (int i = 0; i < picList.size(); i++) {
                DataBean dataBean = new DataBean();
                HomeBannerBean.DataBean.RecordsBean recordsBean = picList.get(i);

                int isMaster = recordsBean.getIsMaster();
                if (!(isMaster == 0 || isMaster == 100000)){
                    continue;
                }

                String picUrl = recordsBean.getPicUrl();
                dataBean.setImageRes(picUrl);
                dataBean.setId(recordsBean.getId());
                dataBeans.add(dataBean);
            }
            Image2Adapter image2Adapter = new Image2Adapter(dataBeans);
            image2Adapter.setBeanDatas(homeBannerBean);

            home_banner
//                .addBannerLifecycleObserver(this)//添加生命周期观察者
                    .setAdapter(image2Adapter)
                    .setIndicator(new CircleIndicator(context))
//                .setIndicator(new RectangleIndicator(this))
                    .isAutoLoop(false)
//                .setIndicatorMargins(new IndicatorConfig.Margins(0,0,0, DpUtil.dip2px(context,50)))
                    .start();
        }catch (Exception e){

        }


        /*if(picList == null)
            return;
        ArrayList<DataBean> dataBeans = new ArrayList<>();

//        CoordinatorLayout

        for (int i = 0; i < picList.size(); i++) {
            DataBean dataBean = new DataBean();
            dataBean.setImageRes(picList.get(i));
            dataBeans.add(dataBean);
        }
        Image2Adapter image2Adapter = new Image2Adapter(dataBeans);
        image2Adapter.setBeanDatas(homeBannerBean);

        home_banner
//                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setAdapter(image2Adapter)
                .setIndicator(new CircleIndicator(context))
//                .setIndicator(new RectangleIndicator(this))
                .isAutoLoop(false)
//                .setIndicatorMargins(new IndicatorConfig.Margins(0,0,0, DpUtil.dip2px(context,50)))
                .start();*/
    }


    //限时购物
   public void  setLimitData(List dataLsit){
       limit_view.setDataList(dataLsit);
    }

    //新品来袭
    public void setNewData(List dataLsit){
        new_view.setDataList(dataLsit);
    }

    //买家推荐
    public void setMaiData(List dataLsit){
        mai_view.setDataList(dataLsit);
    }
    HomeBannerBean homeBannerBean;
    private void getData() {
        String url = DataManager.BaseUrl + "user/papppricture/page";
        /**
         * 去除tokenjjm
         */
//        HashMap<String, String> headMap = new HashMap<>();
//        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(getContext()));
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(getContext(), url, DataManager.GET_TYPE, "", null, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
//                    ((new JSONObject(response)).getJSONObject("data")).getJSONArray("records").getJSONObject(0).getString("picUrl")
                   /* JSONArray ja = ((new JSONObject(response)).getJSONObject("data")).getJSONArray("records");
                    if(ja.length() > 0)
                        picList.clear();
                    for (int i = 0; i < ja.length(); i++) {
                        String picUrl = ja.getJSONObject(i).getString("picUrl");
                        picList.add(picUrl);
                    }*/


                    homeBannerBean = JSON.parseObject(response, HomeBannerBean.class);
                    initBanner();

                }catch (Exception e){

                }
            }
        });
    }

/*    ll_limit_view
    ll_new_view
    ll_mai_view*/

    public void setLimitISVisble(boolean isVisble){
        if(isVisble){
            ll_limit_view.setVisibility(VISIBLE);
        }else{
            ll_limit_view.setVisibility(GONE);
        }
    }


    public void setNewISVisble(boolean isVisble){
        if(isVisble){
            ll_new_view.setVisibility(VISIBLE);
        }else{
            ll_new_view.setVisibility(GONE);
        }
    }


    public void setMaiISVisble(boolean isVisble){
        if(isVisble){
            ll_mai_view.setVisibility(VISIBLE);
        }else{
            ll_mai_view.setVisibility(GONE);
        }
    }


}
